/*
 * TypeName.cpp
 *
 *  Created on: 2018年7月6日
 *      Author: zhengboyuan
 */

#include "TypeName.h"

namespace util
{

const char* toName(TypeName* names, size_t count, int value, bool* found)
{
    for (size_t i = 0; i < count; ++ i)
    {
        if (names[i].type == value)
        {
        	if (found)
        	{
        		*found = true;
        	}

            return names[i].name;
        }
    }

    if (found)
	{
		*found = false;
	}

    return "";
}

int toType(TypeName* names, size_t count, const char* name, size_t len, int defValue)
{
    int value = defValue;
    for (size_t i = 0; i < count; ++ i)
    {
        size_t size = strlen(names[i].name);
        if (size != len)
        {
            continue;
        }

        if (strncmp(name, names[i].name, size) == 0)
        {
            value = names[i].type;
            break;
        }
    }
    return value;
}

bool parseType(TypeName* names, size_t count, const char* name, size_t len, int& value)
{
    bool found = false;
    for (size_t i = 0; i < count; ++ i)
    {
        size_t size = strlen(names[i].name);
        if (size != len)
        {
            continue;
        }

        if (strncmp(name, names[i].name, size) == 0)
        {
            value = names[i].type;
            found = true;
            break;
        }
    }
    return found;
}



}
